package com.inspur.myapplication.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.inspur.myapplication.R;
import com.inspur.myapplication.entity.UserBean;
import com.inspur.myapplication.util.ShoppingDbHelper;
import com.inspur.myapplication.view.LoginActivity;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Administrator on 2020/11/25.
 */

public class GoodsDao {
    private Context context;
    public GoodsDao(Context context){
        this.context=context;
    }

    public ArrayList queryGoods(String name,String category){
        ArrayList arrayList=new ArrayList();

        ShoppingDbHelper shoppingDbHelper=new ShoppingDbHelper(context);
        SQLiteDatabase sqLiteDatabase=shoppingDbHelper.getReadableDatabase();

        try{
            Cursor cursor = sqLiteDatabase.query("goodstb", new String[]{"goodsid", "goodstitle", "price","photo"}, "category=?", new String[]{category}, null, null, null);
            while (cursor.moveToNext()) {
                HashMap hashMap=new HashMap();
                hashMap.put("goodsName",cursor.getString(cursor.getColumnIndex("goodstitle")));
                hashMap.put("goodsPrice",cursor.getString(cursor.getColumnIndex("price")));
                hashMap.put("goodsImg", cursor.getString(cursor.getColumnIndex("photo")));
                hashMap.put("goodsId", cursor.getString(cursor.getColumnIndex("goodsid")));
                arrayList.add(hashMap);
            }
            cursor.close();
        }catch (Exception e){
            Log.e(LoginActivity.class.toString(),e.toString());
        }finally {
            sqLiteDatabase.close();
        }
        return arrayList;
    }
}
